package com.hengtuo.kaiyuanzhongguo;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hengtuo.util.ExceptionApp;
import com.hengtuo.vo.Shark;

/**
 * 摇一摇的详情  http://www.oschina.net/p/fsync?fromerr=jVjqs2hR
 */
public class SharkDetailActivity extends Activity implements View.OnClickListener {

    private String weburl;
    private ImageView back;
    private ImageView menu;
    private RelativeLayout headcontext;
    private WebView webview;
    private RadioButton jianpan;
    private RadioButton text;
    private RadioButton note;
    private RadioButton collction;
    private RadioButton share;
    private LinearLayout linear;
    private ProgressBar progressBar;
    private TextView progerss;
    PopupWindow popupWindow;
    WebSettings webSettings;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shark_detail);

        initialize();

        // webview加载
        getWebView();
    }

    private void getWebView() {

        Intent it = getIntent();
        weburl = it.getStringExtra("url");
        webview.getSettings().setJavaScriptEnabled(true);
        webSettings = webview.getSettings();
        webview.loadUrl(weburl + "?fromerr=jVjqs2hR");
        progerss.setVisibility(View.GONE);
        progressBar.setVisibility(View.GONE);
        webview.setVisibility(View.VISIBLE);


//        webview.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                Log.e("url", "加载中啊····");
//                webview.loadUrl(weburl + "?fromerr=jVjqs2hR");
//                Toast.makeText(SharkDetailActivity.this, "加载中啊", Toast.LENGTH_SHORT).show();
//                progerss.setVisibility(View.GONE);
//                progressBar.setVisibility(View.GONE);
//                webview.setVisibility(View.VISIBLE);
//                return true;
//            }
//        });


    }

    private void initialize() {

        back = (ImageView) findViewById(R.id.shark_detail_back);
        back.setOnClickListener(this);
        menu = (ImageView) findViewById(R.id.shark_detail_menu);
        menu.setOnClickListener(this);
        headcontext = (RelativeLayout) findViewById(R.id.shark_detail_head_context);
        webview = (WebView) findViewById(R.id.shark_detail_webview);
        jianpan = (RadioButton) findViewById(R.id.shark_detail_jianpan);
        jianpan.setOnClickListener(this);
        text = (RadioButton) findViewById(R.id.shark_detail_text);
        text.setOnClickListener(this);
        note = (RadioButton) findViewById(R.id.shark_detail_note);
        note.setOnClickListener(this);
        collction = (RadioButton) findViewById(R.id.shark_detail_collction);
        collction.setOnClickListener(this);
        share = (RadioButton) findViewById(R.id.shark_detail_share);
        share.setOnClickListener(this);
//        linear = (LinearLayout) findViewById(R.id.shark_detail_linear);
//        linear.setOnClickListener(this);
        progressBar = (ProgressBar) findViewById(R.id.shark_detail_cicle_progressbar);
        progerss = (TextView) findViewById(R.id.shark_detail_progress);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shark_detail_back:
                finish();
                break;
            case R.id.shark_detail_menu:
               getPopupWindow();
//                initPopuptWindow();
                // 这里是位置显示方式,在屏幕的又侧
                popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, 740, 100);
                break;
            case R.id.shark_detail_jianpan:// 键盘

                break;
            case R.id.shark_detail_text:// 评论列表

                break;
            case R.id.shark_detail_note:// 说点什么

                break;
            case R.id.shark_detail_collction://收藏

                break;
            case R.id.shark_detail_share:// 分享

                break;
            case R.id.shark_detail_linear:

                break;
        }
    }

    /**
     * 创建PopupWindow
     */
    private void initPopuptWindow() {

        View popupWindow_view = getLayoutInflater().inflate(R.layout.shark_detail_popwindow, null, false);
        // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
        popupWindow = new PopupWindow(popupWindow_view, 300, 400, true);

        TextView refresh = (TextView) popupWindow_view.findViewById(R.id.shark_detail_pop_refresh);
        TextView size = (TextView) popupWindow_view.findViewById(R.id.shark_detail_pop_size);
        // 刷新
        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                progerss.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.VISIBLE);
                webview.setVisibility(View.GONE);
                getWebView();

            }
        });
        // 改变字体大小
        size.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                AlertDialog.Builder builder = new AlertDialog.Builder(SharkDetailActivity.this);
                final String[] typed = {"特大号字", "大号字", "中号字", "小号字"};
                builder.setSingleChoiceItems(typed, 2, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        popupWindow.dismiss();
                        if (typed[which].equals("特大号字")) {
                            webSettings.setTextSize(WebSettings.TextSize.LARGEST);
                        } else if (typed[which].equals("大号字")) {
                            webSettings.setTextSize(WebSettings.TextSize.LARGER);
                        } else if (typed[which].equals("中号字")) {
                            webSettings.setTextSize(WebSettings.TextSize.NORMAL);
                        } else if (typed[which].equals("小号字")) {
                            webSettings.setTextSize(WebSettings.TextSize.SMALLER);
                        }
                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                builder.show();

            }
        });

        // 点击其他地方消失
        popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                return false;
            }
        });


    }

    /***
     * 获取PopupWindow实例
     */
    private void getPopupWindow() {
        if (null != popupWindow) {
            popupWindow.dismiss();
            return;
        } else {
            initPopuptWindow();
        }
    }

}
